/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import persistance.model.User;


/**
 *
 * @author Bobok
 */
public class RegisterJSFBean {

    @EJB
    private UserDAOLocal userDAO;

    private String username;
    private String name;
    private String surname;
    private String email;
    private String email2;
    private String password;
    private String password2;
    private Date birthDate;
    private String captcha;//?

    public Date getBirthDate() {
        return birthDate;
    }

    public String getEmail() {
        return email;
    }

    public String getEmail2() {
        return email2;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public String getPassword2() {
        return password2;
    }

    public String getSurname() {
        return surname;
    }

    public String getUsername() {
        return username;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setEmail2(String email2) {
        this.email2 = email2;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    /** Creates a new instance of RegisterJSFBean */
    public RegisterJSFBean() {
    }

    public String register(){
        boolean validated = true;
        FacesContext ctx = FacesContext.getCurrentInstance();
        FacesMessage msg;

        if (!password2.equals(password)){
            msg = new FacesMessage("Password not match.");
            ctx.addMessage("registerForm:password2", msg);
            validated = false;
        }

        if (!email2.equals(email)){
            msg = new FacesMessage("Email not match.");
            ctx.addMessage("registerForm:email2", msg);
            validated = false;
        }

        if (null != userDAO.getUserByEmail(email)){
            msg = new FacesMessage("Email already exists.");
            ctx.addMessage("registerForm:email", msg);
            validated = false;
        }

        if (!validated){
            return "failure";
        }

        User user = new User();
        user.setEmail(email);
        user.setName(name);
        user.setPassword(password);
        user.setSecurityRole("user");
        user.setUserState(User.State.INACTIVE);
        user.setUsername(username);

        try{
            userDAO.persist(user);
        }catch(Exception ex){
            System.out.println("" + ex + " " + ex.getMessage());
            msg = new FacesMessage("Username already exists, please choose other username.");
            ctx.addMessage("registerForm:username", msg);
            return "failure";
        }
        msg = new FacesMessage("Register successful. Now you can login with you username and password.");
        ctx.addMessage("registerSuccess", msg);
        return "success";
    }

}
